package ljye.com.ljye.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import ljye.com.ljye.R;
import ljye.com.ljye.pagers.PhotoViewPager;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;
//图片浏览
public class Activity_ImageBrowse extends Activity {

    private List<String> imgs;

    private int position;
    private TextView tv;
    private PhotoViewPager viewpager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_browse);
        this.position = getIntent().getIntExtra("position", 0);

        this.imgs = getIntent().getStringArrayListExtra("imgs");

        viewpager = (PhotoViewPager) findViewById(R.id.imgs_viewpager);

        tv = (TextView) this.findViewById(R.id.tv);
        viewpager.setOffscreenPageLimit(2);


        PagerAdapter adapter = new MyViewPagerAdapter(this, imgs);
        viewpager.setAdapter(adapter);
        viewpager.setCurrentItem(position);
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tv.setText(position + 1 + "/" + imgs.size());

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    public class MyViewPagerAdapter extends PagerAdapter {
        boolean isfash = true;

        List<String> imgs;

        Context mContext;

        public MyViewPagerAdapter(Context context, List<String> imgs) {

            this.mContext = context;
            this.imgs = imgs;

        }

        @Override
        public int getCount() { // 获得size
            return imgs.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, final int position) {
            if (isfash) {
                tv.setText(position + 1 + "/" + imgs.size());
                isfash = false;
            }

            String imgUrl = imgs.get(position);
            LinearLayout view = (LinearLayout) LayoutInflater.from(mContext).inflate(R.layout.img_browse, null);
            final PhotoView img = (PhotoView) view.findViewById(R.id.img_plan);
            img.setTag(imgUrl);
            Picasso.with(Activity_ImageBrowse.this)
                    .load(imgs.get(position))
                    .placeholder(R.drawable.ic_default_image)
                    .into(img);
            ((ViewPager) container).addView(view);
            img.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View view, float x, float y) {
                    onBackPressed();
                }
            });

            return view;

        }

    }
}
